<?php
namespace patterns;
/**
 * Adapter pattern. Manages unknown functionality.
 *
 */
class Adapter
{
	protected $lib = null;
	//abstract public function __construct();

	public function __call($method, $parameters)
	{
		if (!isset($this->lib))
		{
			die("Lib not set");
		}

		if (!is_array($parameters))
		{
			die("Bad parameters for adapter");
		}

		if (method_exists($this, $method))
		{
			return call_user_func_array(array($this, $method), $parameters);
		}
		elseif (method_exists($this->lib, $method))
		{
			return call_user_func_array(array($this->lib, $method), $parameters);
		}
		else
		{
			die("Method $method not set in service ".get_class($this->lib));
			//core("debug")->error("Method $method not set in service ".get_class($this->lib));
		}
	}

	protected function SetLib($lib)
	{
		if (is_null($this->lib))
		{
			$this->lib = $lib;
			return true;
		}
		else
		{
			return false;
		}
	}
}